package com.baor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baor.dao.DistributionConfDao;
import com.baor.domain.conf.DistributionConf;
import com.baor.service.DistributionConfService;
import com.baor.utils.AjaxResult;
import com.baor.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DistributionConfServiceImpl implements DistributionConfService {

    @Autowired
    private DistributionConfDao confDao;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 完全就是废物方法，禁止调用
     * 但是我写了，我就是不想删
     * @return
     */

    @Override
    public AjaxResult<?> insert() {
        DistributionConf vo = new DistributionConf();
        vo.setConfOpen(0);
        vo.setConfLevel(0);
        vo.setConfExamine(0);
        vo.setConfSelf(0);
        vo.setConfWays(0);
        vo.setConfMethod(0);
        vo.setConfDistributionCondition(0);
        vo.setConfCompletion(0);
        vo.setConfConsumeCondition(0);
        vo.setConfHide(0);
        vo.setProtocalId(0);
        int insert = confDao.insert(vo);
        redisUtil.setValue("disConf-site"+vo.getSiteId().toString(),vo);
        return insert > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @Override
    public AjaxResult<?> update(DistributionConf vo) {
        int i = confDao.update(vo,new LambdaQueryWrapper<DistributionConf>()
                .eq(DistributionConf::getSiteId, vo.getSiteId()));
        if ( i > 0 ){
            redisUtil.setValue("disConf-site-"+vo.getSiteId().toString(),vo);
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @Override
    public AjaxResult<?> info(Integer id) {
        Object value = redisUtil.getValue("disConf-site-" + id);
        if ( value != null )
            return AjaxResult.success((DistributionConf)value);
        DistributionConf conf = confDao.selectOne(new LambdaQueryWrapper<DistributionConf>()
                .eq(DistributionConf::getSiteId, id));
        if ( conf != null )
            return  AjaxResult.success(conf);
        return AjaxResult.error();
    }
}
